{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "7e25890c-4e66-4d6e-9f76-ad699bab6183",
   "metadata": {},
   "outputs": [],
   "source": [
    "import os\n",
    "import cv2\n",
    "import numpy as np\n",
    "import pandas as pd\n",
    "import matplotlib.pyplot as plt\n",
    "import json"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "a698fdcd-154a-4df3-b1e6-457e6f9ebaad",
   "metadata": {},
   "outputs": [],
   "source": [
    "# 选择图片和标签数据\n",
    "target = 3\n",
    "data_name = ['0618', '0854', '1066'][target - 1]\n",
    "img_path = f'../input_data/{data_name}.png'  # 原图路径\n",
    "json_path = f'../input_data/{data_name}.json'  # Labelme 标注文件路径\n",
    "output_features_filename = f'./Features/{data_name}_HSV3x3_features.npy'  # 数据输出路径\n",
    "output_labels_filename = f'./Features/{data_name}_HSV3x3_labels.npy'  # 数据输出路径"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "id": "b4d9adf9-0d31-4cc2-9421-3179feed3c75",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Features saved to ./Features/1066_HSV3x3_features.npy       \n",
      " Labels saved to ./Features/1066_HSV3x3_labels.npy.\n"
     ]
    }
   ],
   "source": [
    "# 加载原图\n",
    "img = cv2.imread(img_path)\n",
    "\n",
    "# 转为HSV图像\n",
    "img_hsv = cv2.cvtColor(img, cv2.COLOR_BGR2HSV)\n",
    "\n",
    "# 读取 Labelme JSON 数据\n",
    "with open(json_path) as f:\n",
    "    label_data = json.load(f)\n",
    "\n",
    "# 创建空的标签图\n",
    "height, width, _ = img.shape\n",
    "label_img = np.zeros((height, width), dtype=np.uint8)\n",
    "\n",
    "# 根据多边形标注生成标签图\n",
    "for shape in label_data['shapes']:\n",
    "    label = shape['label']\n",
    "    points = np.array(shape['points'], dtype=np.int32)\n",
    "    if label == 'road':\n",
    "        cv2.fillPoly(label_img, [points], 255)  # 道路: road\n",
    "    elif label == 'other':\n",
    "        cv2.fillPoly(label_img, [points], 0)   # 其他: other \n",
    "    elif label == 'bg':\n",
    "        cv2.fillPoly(label_img, [points], 128)    # 主背景: bg\n",
    "\n",
    "# 确定采样步长\n",
    "step_size = 10  # 增大步长以减少采样点数量\n",
    "\n",
    "# 数据暂存\n",
    "features = []\n",
    "labels = []\n",
    "sampling_points = []\n",
    "\n",
    "# 进行均匀采样\n",
    "for y in range(1, height - 1, step_size):\n",
    "    for x in range(1, width - 1, step_size):\n",
    "        # 提取3x3区域的HSV值\n",
    "        feature_vector = img_hsv[y-1:y+2, x-1:x+2].flatten()  # 3x3平铺\n",
    "        features.append(feature_vector)\n",
    "\n",
    "        # 根据标签图确定标签\n",
    "        label_value = label_img[y, x]\n",
    "        if label_value == 255:  # road\n",
    "            labels.append(1)\n",
    "        elif label_value == 0:  # other\n",
    "            labels.append(2)\n",
    "        else:\n",
    "            labels.append(0)  # background\n",
    "        \n",
    "        # 记录采样点位置\n",
    "        sampling_points.append((x, y))\n",
    "\n",
    "# 转换为NumPy数组\n",
    "features_array = np.array(features)\n",
    "labels_array = np.array(labels)\n",
    "\n",
    "# 保存数据到npy文件\n",
    "np.save(output_features_filename, features_array)\n",
    "np.save(output_labels_filename, labels_array)\n",
    "\n",
    "print(f\"Features saved to {output_features_filename} \\\n",
    "      \\n Labels saved to {output_labels_filename}.\")\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "1f3ad691-36f1-44fc-9e14-73f0d616c06a",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.9.20"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
